const xlsx = require("node-xlsx");
const fs = require("fs");
const path = require("path");

module.exports = (req, res, next)=>{
  // 模板文件根目录
  const formworkPath = path.join(__dirname, "..", "..", "formwork");
  
  // 是否存在用户信息模板
  const dirData = fs.readdirSync(formworkPath);
  // 不存在，则创建
  if(!dirData.includes("userformwork.xlsx")){
    // 将数据打包成文件
    const buffer = xlsx.build([{name: "工作表1", data: [[
      "tel(必填)", "uName", "age", "sex", "power(必填)", "tId(必填)"
    ]] }]);
    fs.writeFileSync( path.join(formworkPath, "userformwork.xlsx") , buffer );
  }

  // 向前端响应模板文件
  res.sendFile(path.join(formworkPath, "userformwork.xlsx"));
}